/**
 * API服务类
 */
const { get, post } = require('../utils/request');

/**
 * 用户相关API
 */
const userApi = {
  /**
   * 微信登录
   * @param {Object} data - 登录参数
   * @returns {Promise} Promise对象
   */
  wxLogin: (data) => {
    return post('/api/user/wx-login', data);
  },
  
  /**
   * 获取用户信息
   * @returns {Promise} Promise对象
   */
  getUserInfo: () => {
    return get('/user/info');
  }
};

/**
 * 设备数据相关API
 */
const deviceApi = {
  /**
   * 获取今日空气质量数据
   * @returns {Promise} Promise对象
   */
  getTodayData: () => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: {
            score: 120,
            level: '良',
            temperature: 24.5,
            humidity: 55,
            co2: 450,
            pm25: 15,
            formaldehyde: 0.03
          }
        });
      }, 300 + Math.random() * 200); // 添加随机延迟模拟网络请求
    });
  },
  
  /**
   * 获取昨日空气质量数据
   * @returns {Promise} Promise对象
   */
  getYesterdayData: () => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: {
            score: 88,
            level: '良',
            temperature: 23.8,
            humidity: 60,
            co2: 480,
            pm25: 22,
            formaldehyde: 0.04
          }
        });
      }, 300 + Math.random() * 200); // 添加随机延迟模拟网络请求
    });
  },
  
  /**
   * 获取设备连接状态 - 模拟数据
   * @returns {Promise} Promise对象
   */
  getDeviceStatus: () => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: {
            isConnected: true,
            lastConnectTime: new Date().toISOString(),
            batteryLevel: 85,
            signalStrength: 'good',
            deviceId: 'DEVICE_001',
            deviceName: '客厅空气检测仪'
          }
        });
      }, 300);
    });
  },
  
  /**
   * 获取可绑定设备列表 - 模拟数据
   * @returns {Promise} Promise对象
   */
  getDeviceList: () => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: [
            { deviceId: 'DEVICE_002', deviceName: '卧室空气检测仪', status: 'available' },
            { deviceId: 'DEVICE_003', deviceName: '书房空气检测仪', status: 'available' },
            { deviceId: 'DEVICE_004', deviceName: '厨房空气检测仪', status: 'available' }
          ]
        });
      }, 300);
    });
  },
  
  /**
   * 获取已绑定设备列表 - 模拟数据
   * @returns {Promise} Promise对象
   */
  getBoundDevices: () => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: [
            { deviceId: 'DEVICE_001', deviceName: '客厅空气检测仪', status: 'connected' }
          ]
        });
      }, 300);
    });
  },
  
  /**
   * 绑定设备 - 模拟数据
   * @param {Object} data - 设备信息
   * @returns {Promise} Promise对象
   */
  bindDevice: (data) => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        console.log('模拟绑定设备:', data);
        resolve({
          code: 0,
          message: '设备绑定成功',
          data: {
            deviceId: data.deviceId,
            deviceName: data.deviceName,
            bindTime: new Date().toISOString()
          }
        });
      }, 500);
    });
  },  
  
  /**
   * 获取历史数据
   * @param {Object} params - 查询参数
   * @returns {Promise} Promise对象
   */
  getHistoryData: (params) => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: [
            { date: '2023-06-01', score: 95, level: '优', pm25: 12 },
            { date: '2023-06-02', score: 88, level: '良', pm25: 18 },
            { date: '2023-06-03', score: 76, level: '良', pm25: 25 },
            { date: '2023-06-04', score: 92, level: '优', pm25: 14 },
            { date: '2023-06-05', score: 85, level: '良', pm25: 20 }
          ]
        });
      }, 300 + Math.random() * 200); // 添加随机延迟模拟网络请求
    });
  },
  
  /**
   * 获取指定日期的详细数据
   * @param {string} date - 日期字符串 (YYYY-MM-DD)
   * @returns {Promise} Promise对象
   */
  getDateDetail: (date) => {
    // 返回模拟数据而不是调用API
    return new Promise((resolve) => {
      setTimeout(() => {
        console.log('获取日期详情:', date);
        resolve({
          data: {
            date: date,
            score: 85 + Math.floor(Math.random() * 20),
            level: '良',
            temperature: 23 + Math.random() * 5,
            humidity: 50 + Math.random() * 20,
            co2: 400 + Math.random() * 100,
            pm25: 15 + Math.random() * 15,
            formaldehyde: 0.02 + Math.random() * 0.03,
            hourlyData: [
              { hour: '00:00', pm25: 12, temperature: 22 },
              { hour: '04:00', pm25: 10, temperature: 21 },
              { hour: '08:00', pm25: 15, temperature: 23 },
              { hour: '12:00', pm25: 20, temperature: 26 },
              { hour: '16:00', pm25: 18, temperature: 25 },
              { hour: '20:00', pm25: 14, temperature: 23 }
            ]
          }
        });
      }, 300 + Math.random() * 200); // 添加随机延迟模拟网络请求
    });
  }
};

// 获取今日空气质量数据
export function getTodayAirQuality() {
  return get('/api/airquality/today');
}

// 获取历史空气质量数据
export function getHistoryAirQuality(params) {
  return get('/api/airquality/history', params);
}

// 获取最新实时数据
export function getRealtimeAirQuality() {
  // 返回模拟数据而不是调用API
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        data: {
          score: Math.floor(80 + Math.random() * 40),  // 随机生成80-120之间的分数
          level: '良',
          temperature: (20 + Math.random() * 10).toFixed(1),  // 随机生成20-30之间的温度
          humidity: Math.floor(40 + Math.random() * 30),  // 随机生成40-70之间的湿度
          co2: Math.floor(400 + Math.random() * 200),  // 随机生成400-600之间的二氧化碳
          pm25: Math.floor(10 + Math.random() * 20),  // 随机生成10-30之间的PM2.5
          formaldehyde: (0.01 + Math.random() * 0.05).toFixed(2)  // 随机生成0.01-0.06之间的甲醛
        }
      });
    }, 500 + Math.random() * 300);  // 添加随机延迟模拟网络请求
  });
}

// 移除获取实时数据时间段接口，按照用户要求
// export function getRealtimeAirQualityRange(params) {
//   return get('/api/airquality/realtime/range', params);
// }

module.exports = {
  userApi,
  deviceApi,
  getTodayAirQuality,
  getHistoryAirQuality,
  getRealtimeAirQuality
};